package artid.com.arttogo.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.android.volley.VolleyError;

import java.util.ArrayList;
import java.util.List;

import artid.com.arttogo.R;
import artid.com.arttogo.adapter.ArtistRanking_Adapter;
import artid.com.arttogo.bean.ArtistRankingParser;
import artid.com.arttogo.db.Cache;
import artid.com.arttogo.network.VolleyBasic;
import artid.com.arttogo.network.VolleyCallback;
import artid.com.arttogo.network.VolleyData;
import artid.com.arttogo.network.VolleyMap;
import artid.com.arttogo.view.pulllist.PullToRefreshLayout;
import artid.com.arttogo.view.pulllist.PullableListView;

/**
 * 
 *    
 * 项目名称：ArtworkArt   
 * 类名称：ArtistRanking_Activity   
 * 类描述：艺术家排名   排行榜
 * 创建人：耿卫斌   
 * 创建时间：2017-1-12 下午2:37:52   
 *
 */
public class ArtistRanking_Activity extends BaseActivity implements OnClickListener {
	private Context mContext;
	private PullableListView mListView;
	private PullToRefreshLayout mPullLayout;
	private boolean isRefreshed = false;// 是否刷新
	private int page=1;
	private List<ArtistRankingParser> list;
	private List<ArtistRankingParser> more_list;
	private ArtistRanking_Adapter adapter;
	private LinearLayout noDataLL;//无数据布局
	private String carousel_id="1";//轮播图id
	private String title="";//排名标题
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_artistranking);
		mContext=ArtistRanking_Activity.this;
		initView();
	}

	private void initView(){
		carousel_id = getIntent().getExtras().getString("id"); 
		title = getIntent().getExtras().getString("title");
		list=new ArrayList<ArtistRankingParser>();
		more_list=new ArrayList<ArtistRankingParser>();
		mBack = (ImageView) findViewById(R.id.back_btn);
		mBack.setVisibility(View.VISIBLE);
		mBack.setOnClickListener(this);
		mTittle = (TextView) findViewById(R.id.tittle_text);
		if(!TextUtils.isEmpty(title)){
			mTittle.setText(title);
		}else{
			mTittle.setText("艺术家排行");
		}
		noDataLL = (LinearLayout) findViewById(R.id.ll_hint);
		mListView=(PullableListView) findViewById(R.id.ArtistRanking_ListView);
		mPullLayout=(PullToRefreshLayout) findViewById(R.id.ArtistRanking_refresh_view);
		mPullLayout.setOnRefreshListener(new PullToRefreshLayout.OnRefreshListener() {

			@Override
			public void onRefresh(PullToRefreshLayout pullToRefreshLayout) {
				// TODO Auto-generated method stub
				//下拉刷新
				page=1;
				isRefreshed=true;
				getData();
			}

			@Override
			public void onLoadMore(PullToRefreshLayout pullToRefreshLayout) {
				// TODO Auto-generated method stub
				// pullToRefreshLayout.setBottomDismiss();
				//上拉加载
				page++;
				getData();
			}
		}); 
		getData();
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.back_btn:
			finish();
			break;

		default:
			break;
		}
	}

	/**
	 * 获取艺术家信息和作品信息。
		页数	page
		条数	pagesize
		轮播图id	carousel_id
	 */
	private void getData(){
		VolleyBasic.getInstance(this).post(Cache.bannerList,
				VolleyMap.getInstance().bannerList(page, 10, carousel_id),
				ArtistRankingParser.class, new VolleyCallback() {

			@Override
			public void onSuccess(VolleyData vd) {
				// TODO Auto-generated method stub
				mPullLayout.loadmoreFinish(mPullLayout.SUCCEED);
				list=(List<ArtistRankingParser>) vd.getList();
				if (isRefreshed == true) {
					more_list.clear();
					isRefreshed = false;
				}
				if (!list.isEmpty()) {
					more_list.addAll(list);
				}
				//这里是显示有数据和无数据的布局
				if(more_list!=null && more_list.size()!=0){
					noDataLL.setVisibility(View.GONE);
				}else{
					noDataLL.setVisibility(View.VISIBLE);
					return;
				}

				if (adapter == null) {
					adapter=new ArtistRanking_Adapter(mContext, more_list);
					mListView.setAdapter(adapter);
					mListView.setOnItemClickListener(new OnItemClickListener() {

						@Override
						public void onItemClick(AdapterView<?> arg0, View arg1,
												int arg2, long arg3) {
							// TODO Auto-generated method stub
							Intent intent=new Intent(mContext,CollecterActivity.class);
							intent.putExtra("name", more_list.get(arg2).getRealname());
							intent.putExtra("id", more_list.get(arg2).getArtist_id());
							mContext.startActivity(intent);
						}
					});
				}
				adapter.notifyDataSetChanged();
			}

			@Override
			public void onError(VolleyError volleyError) {
				// TODO Auto-generated method stub
				mPullLayout.loadmoreFinish(mPullLayout.FAIL);
				//这里是显示有数据和无数据的布局
				if(more_list!=null && more_list.size()!=0){
					noDataLL.setVisibility(View.GONE);
				}else{
					noDataLL.setVisibility(View.VISIBLE);
					return;
				}
			}
		});
	}
}
